package com.xywy.tangandroid.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.adapter.MyCollectAdapter;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.MyCollectionItem;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.custom.MyLisview;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.MD5;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;

/*******************************************
 * @Title: MyCollectionAct.java
 * @Package com.xywy.tangandroid.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author Benwolf 修改
 * @date 2014-12-4 下午4:22:26
 * @version V1.0
 ********************************************/
public class MyCollectionAct extends BaseActivity {
	private RelativeLayout left_img;
	private TextView middle_title;
	private MyLisview mylistview;
	private List<MyCollectionItem> list = new ArrayList<MyCollectionItem>();;
	private MyCollectAdapter<MyCollectionItem> adapter;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_my_collection);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(R.string.my_collection));
		mylistview = (MyLisview) findViewById(R.id.mylistview);

	}

	@Override
	protected void initData() {
		String sign = null;
		sign = MD5.md5s("KHy69gsk8%#@kl$" + "TangAndroid");
		System.out.println(">>>>>>>>>>>>>>>>>>" + sign);
		
		String url =Constant.getCOllection_url;
		PostRequest<String> postRequest = new PostRequest<String>(url
				, String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context, context.getResources()
								.getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {

						UIUtil.DissDialog(context, waittingDialog);
						try {
							if (response != null) {
								JSONObject json = new JSONObject(response);
								int state = json.optInt("state");
								if (state == 200) {
									Toast.makeText(context, "获取收藏列表成功！",
											Toast.LENGTH_SHORT).show();
									Gson gson = new Gson();
									list = gson
											.fromJson(
													json.optJSONArray("data")
															.toString(),
													new TypeToken<List<MyCollectionItem>>() {
													}.getType());
									System.out.println("list 的size "
											+ list.size());
									if (list.size() > 0) {
										findViewById(R.id.no_collection_promot)
												.setVisibility(View.GONE);
									}

									// adapter.notifyDataSetChanged();
									adapter = new MyCollectAdapter<MyCollectionItem>(
											MyCollectionAct.this, list);

									mylistview.setAdapter(adapter);
									mylistview
											.setOnItemClickListener(new OnItemClickListener() {

												@Override
												public void onItemClick(
														AdapterView parent,
														View v, int position,
														long id) {
													// TODO Auto-generated
													// method stub
													Intent intent = new Intent(
															context,
															ExpertsArticleAct.class);
													intent.putExtra("id", list
															.get(position)
															.getDocument_id());
													intent.putExtra(
															"cateid",
															list.get(position)
																	.getCategoryid());
													intent.putExtra(
															"document_id",
															list.get(position)
																	.getDocument_id());
													context.startActivity(intent);

												}

											});

								} else {
									Toast.makeText(MyCollectionAct.this,
											json.optString("error"), 1).show();
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				});
		HashMap<String, String> params = new HashMap<String, String>();
		//+ "sign=" + sign + "&tag="
//		+ "TangAndroid"+"&xywy_userid="+User.getIUserInstance(context).getUserid()+"&appid=2"
		params.put("sign", sign);
		params.put("tag", "TangAndroid");
		params.put("xywy_userid", User.getIUserInstance(context).getUserid());
		params.put("app_id", 2+"");
		postRequest.setIsParseJson(false);
		postRequest.setParams(params);
		executeRequest(postRequest);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		left_img.setOnClickListener(this);

	}
}
